package com.hkts.management.unattended.config;

import com.hkts.management.unattended.poundroom.entity.*;
import com.hkts.management.unattended.poundroom.service.serviceimpl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SyncPoundRoomUtil {
    @Autowired
    private YbMutualCompanyServiceImpl ybMutualCompanyService;
    @Autowired
    private YbMutualCompanySlaveServiceImpl ybMutualCompanySlaveService;
    @Autowired
    private YbMutualInventoryServiceImpl ybMutualInventoryService;
    @Autowired
    private YbMutualInventorySlaveServiceImpl ybMutualInventorySlaveService;
    @Autowired
    private YbMutualPovouchServiceImpl ybMutualPovouchService;
    @Autowired
    private YbMutualPovouchSlaveServiceImpl ybMutualPovouchSlaveService;
    @Autowired
    private YbMutualSovouchServiceImpl ybMutualSovouchService;
    @Autowired
    private YbMutualSovouchSlaveServiceImpl ybMutualSovouchSlaveService;
    @Autowired
    private YbMutualWarehouseServiceImpl ybMutualWarehouseService;
    @Autowired
    private YbMutualWarehouseSlaveServiceImpl ybMutualWarehouseSlaveService;
    @Autowired
    private YbMutualInventoryclassServiceImpl ybMutualInventoryclassService;
    @Autowired
    private YbMutualInventoryclassSlaveServiceImpl ybMutualInventoryclassSlaveService;
//    @Async
    public void inventoryclass(){
        System.out.println("=====================同步物料类别数据开始========================");
//        LocalDateTime inventoryclassLastUpdateTime= ybMutualInventoryclassService.getLastUpdateTime();
//        Integer inventoryclassCount=ybMutualInventoryclassSlaveService.getUpdateTimeCount(inventoryclassLastUpdateTime);
        Integer inventoryclassCount=ybMutualInventoryclassSlaveService.getBmutualStatusCount();
        int flag=0;
        for (Integer i=inventoryclassCount;i>0;i--){
//            List<YbMutualInventoryclass> inventoryclass= ybMutualInventoryclassSlaveService.getUpdateTimeData(inventoryclassLastUpdateTime,flag);
            List<YbMutualInventoryclass> inventoryclass= ybMutualInventoryclassSlaveService.getBmutualStatuData(flag);
            if(inventoryclass.size()>0){
                inventoryclass=inventoryclass.stream().peek(t -> t.setBmutualStatus(true)).collect(Collectors.toList());
                ybMutualInventoryclassService.mysqlInsertOrUpdateBath(inventoryclass);
                ybMutualInventoryclassSlaveService.updateBatchById(inventoryclass);
            }
            flag++;
        }
        System.out.println("=====================同步物料类别数据结束========================");
    }
//    @Async
    public void ybMutualCompanies(){
        System.out.println("=====================同步公司数据开始========================");
//        LocalDateTime DoptTime= ybMutualCompanyService.getLastMutualCompany();
        Integer ybMutualCompaniesCount=ybMutualCompanySlaveService.getBmutualStatusCount();
        int flag=0;
        for (Integer i=ybMutualCompaniesCount;i>0;i--){
            List<YbMutualCompany> ybMutualCompanies= ybMutualCompanySlaveService.getBmutualStatuData(flag);
            if(ybMutualCompanies.size()>0){
                ybMutualCompanies=ybMutualCompanies.stream().peek(t -> t.setBmutualStatus(true)).collect(Collectors.toList());
                ybMutualCompanyService.mysqlInsertOrUpdateBath(ybMutualCompanies);
                ybMutualCompanySlaveService.updateBatchById(ybMutualCompanies);
            }
            flag++;
        }
        System.out.println("=====================同步公司数据结束========================");
    }
//    @Async
    public void inventories(){
        System.out.println("=====================同步物料数据开始========================");
//        LocalDateTime inventoryDoptTime= ybMutualInventoryService.getLastUpdateTime();
//        Integer inventoriesCount=ybMutualInventorySlaveService.getUpdateTimeCount(inventoryDoptTime);
        Integer inventoriesCount=ybMutualInventorySlaveService.getBmutualStatusCount();
        int flag=0;
        for (Integer i=inventoriesCount;i>0;i--){

            List<YbMutualInventory> inventories= ybMutualInventorySlaveService.getBmutualStatuData(flag);
            if(inventories.size()>0){
                inventories=inventories.stream().peek(t -> t.setBmutualStatus(true)).collect(Collectors.toList());
                ybMutualInventoryService.mysqlInsertOrUpdateBath(inventories);
                ybMutualInventorySlaveService.updateBatchById(inventories);
            }
            flag++;
        }
        System.out.println("=====================同步物料数据结束========================");

    }
//    @Async
    public void povouchServiceLast(){
        System.out.println("=====================同步销售数据开始========================");
//        LocalDateTime povouchServiceLastUpdateTime= ybMutualPovouchService.getLastUpdateTime();
        Integer povouchesCount=ybMutualPovouchSlaveService.getBmutualStatusCount();
        int flag=0;
        for (Integer i=povouchesCount;i>0;i--){

            List<YbMutualPovouch> povouches= ybMutualPovouchSlaveService.getBmutualStatuData(flag);
            if(povouches.size()>0){
                povouches=povouches.stream().peek(t -> t.setBmutualStatus(true)).collect(Collectors.toList());
                ybMutualPovouchService.mysqlInsertOrUpdateBath(povouches);
                ybMutualPovouchSlaveService.updateBatchById(povouches);
            }
            flag++;
        }
        System.out.println("=====================同步销售数据结束========================");

    }
//    @Async
    public void sovouches(){
        System.out.println("=====================同步采购数据开始========================");
//        LocalDateTime serviceLastUpdateTime= ybMutualSovouchService.getLastUpdateTime();
        Integer sovouchesCount=ybMutualSovouchSlaveService.getBmutualStatusCount();
        int flag=0;
        for (Integer i=sovouchesCount;i>0;i--){

            List<YbMutualSovouch> sovouches= ybMutualSovouchSlaveService.getBmutualStatuData(flag);
            if(sovouches.size()>0){
                sovouches=sovouches.stream().peek(t -> t.setBmutualStatus(true)).collect(Collectors.toList());
                ybMutualSovouchService.mysqlInsertOrUpdateBath(sovouches);
                ybMutualSovouchSlaveService.updateBatchById(sovouches);
            }
            flag++;
        }
        System.out.println("=====================同步采购数据结束========================");

    }
//    @Async
    public void warehouseLast(){
        System.out.println("=====================同步仓库数据开始========================");
//        LocalDateTime warehouseLastUpdateTime= ybMutualWarehouseService.getLastUpdateTime();
        Integer warehousesCount=ybMutualWarehouseSlaveService.getBmutualStatusCount();
        int flag=0;
        for (Integer i=warehousesCount;i>0;i--){
            List<YbMutualWarehouse> warehouses= ybMutualWarehouseSlaveService.getBmutualStatuData(flag);
            if(warehouses.size()>0){
                warehouses=warehouses.stream().peek(t -> t.setBmutualStatus(true)).collect(Collectors.toList());
                ybMutualWarehouseService.mysqlInsertOrUpdateBath(warehouses);
                ybMutualWarehouseSlaveService.updateBatchById(warehouses);
            }
            flag++;
        }
        System.out.println("=====================同步仓库数据结束========================");

    }
}
